layui.use(['table', 'admin', 'ax'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var form = layui.form;
    var layer = layui.layer;


    /**
     * 周转容器管理管理
     */
    var TurnoverContainer = {
        tableId: "turnoverContainerTable"
    };

    /**
     * 初始化表格的列
     */
    TurnoverContainer.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键id'},
            {align: 'center', toolbar: '#tableBar', title: '操作'},
            {field: 'packNumber', sort: true, title: '容器编号'},
            {field: 'storageState', sort: true, title: '存储状态'},
            {field: 'attribute', sort: true, title: '类型'},
            {field: 'specifications', sort: true, title: '规格'},
            {field: 'operator', sort: true, title: '操作人'},
            {field: 'sendTime', sort: true, title: '操作时间'},
            {field: 'warehouseName', sort: true, title: '仓库名称'},
            {field: 'customerName', sort: true, title: '门店'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    TurnoverContainer.search = function () {
        var queryData = {};
        queryData['storageState'] = $("#storageState").val();
        queryData['attribute'] = $("#attribute").val();
        queryData['condition'] = $("#condition").val();
        queryData['warehouseId'] = $("#areaWarehouseId").val();
        console.log(queryData);
        table.reload(TurnoverContainer.tableId, {
            where: queryData, page: {curr: 1},
             done:function () {
                 // 搜索按钮点击事件
                 $('#btnSearch').click(function () {
                     TurnoverContainer.search();
                 });
                 // 添加按钮点击事件
                 $('#btnAdd').click(function () {
                     TurnoverContainer.openAddDlg();
                 });
                 // 导出excel
                 $('#btnExp').click(function () {
                     TurnoverContainer.exportExcel();
                 });
            }
        });
    };

    // 导入
    $('#btnExpimport').click(function () {
        layer.open({
            type: 2,
            title: '容器编号导入',
            area: ['1000px', '750px'],
            anim: 5,
            content: Feng.ctxPath + '/turnoverContainer/toimportExcel',
            end: function () {
                table.reload(TurnoverContainer.tableId);
            }
        });
    });
    /**
     * 弹出添加对话框
     */
    TurnoverContainer.openAddDlg = function () {
        window.location.href = Feng.ctxPath + '/turnoverContainer/add';
    };

    /**
     * 导出excel按钮
     */
    TurnoverContainer.exportExcel = function () {
        var checkRows = table.checkStatus(TurnoverContainer.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    TurnoverContainer.openEditDlg = function (data) {
        window.location.href = Feng.ctxPath + '/turnoverContainer/edit?id=' + data.id;
    };

    /**
     * 查看明细
     *
     * @param data 点击按钮时候的行数据
     */
    TurnoverContainer.onDetailDe = function (data) {
        localStorage.setItem('packNumber', JSON.stringify({ key: data.packNumber }));
        window.location.href = Feng.ctxPath + '/turnoverContainerDetailed';

    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    TurnoverContainer.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/turnoverContainer/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(TurnoverContainer.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + TurnoverContainer.tableId,
        url: Feng.ctxPath + '/turnoverContainer/list',
        page: true,
        limit: 20,
        height: "full-60",
        cols:TurnoverContainer.initColumn(),
        toolbar: '#toolbarfrom',
        cellMinWidth: 75,
    });

    $('#btnPrint').click(function () {
        var checkStatus = table.checkStatus('turnoverContainerTable');
        data = checkStatus.data;
        if (data.length > 0) {
            var ids = [];
            for (var i = 0; i < data.length; i++) {
                ids.push(data[i].packNumber);
            }
            layer.open({
                type: 2,
                title: '批量打印容器码',
                area: ['900px', '550px'],
                anim: 5,
                content: Feng.ctxPath + '/turnoverContainer/tobtnqrCode?ids=' + ids,
                end: function () {
                }
            });
        } else {
            layer.alert('请至少选中一条数据');
        }
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        TurnoverContainer.search();
    });

    // 导出excel
    $('#btnExp').click(function () {
        TurnoverContainer.exportExcel();
    });
    $('#btnAdd').click(function () {
        TurnoverContainer.openAddDlg();
    });


    // 工具条点击事件
    table.on('tool(' + TurnoverContainer.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            TurnoverContainer.openEditDlg(data);
        } else if (layEvent === 'delete') {
            TurnoverContainer.onDeleteItem(data);
        } else if (layEvent === 'detail') {
            TurnoverContainer.onDetailDe(data);
        }
    });
});
